package com.hoko.blur.util;

import ohos.agp.components.Component;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.Matrix;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

public class BitmapUtil {
    public static PixelMap getScaledBitmap(PixelMap bitmap, float factor) {
        if (bitmap == null) {
            return null;
        }

        if (factor == 1.0f) {
            return bitmap;
        }

        final int width = bitmap.getImageInfo().size.width;
        final int height = bitmap.getImageInfo().size.height;

        final float ratio = 1f / factor;

        Matrix matrix = new Matrix();
        matrix.postScale(ratio, ratio);//x,y轴的缩放为ratio

        PixelMap.InitializationOptions opt = new PixelMap.InitializationOptions();
        opt.size = new Size((int) (bitmap.getImageInfo().size.width * ratio), (int) (bitmap.getImageInfo().size.height * ratio));
        opt.editable = true;
        opt.pixelFormat = PixelFormat.ARGB_8888;
        Rect srcRegion = new Rect(0, 0, 0, 0);
        return PixelMap.create(bitmap, srcRegion, opt);
//        return PixelMap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    }

    /**
     * getViewBitmap
     *
     * @param view q
     * @param translateX q
     * @param translateY q
     * @param sampleFactor q
     * @return q
     */
    public static PixelMap getViewBitmap(Component view, int translateX, int translateY, float sampleFactor) {
        final float scale = 1.0f / sampleFactor;

        final int width = view.getWidth();
        final int height = view.getHeight();
        final int downScaledWidth = (int) (width * scale);
        final int downScaledHeight = (int) ((height - translateY) * scale);

        PixelMap.InitializationOptions opt = new PixelMap.InitializationOptions();
        opt.size = new Size(downScaledWidth, downScaledHeight);
//        opt.size.width = downScaledWidth; // 宽缩放ratio
//        opt.size.height = downScaledHeight; // 搞缩放ration
        opt.editable = true;
        opt.pixelFormat = PixelFormat.ARGB_8888;

        PixelMap bitmap = PixelMap.create(opt);

        if (view.getBackgroundElement() != null && view.getBackgroundElement() instanceof ShapeElement) {
            bitmap.writePixels(((ShapeElement) view.getBackgroundElement()).getRgbColors()[0].asArgbInt());
        } else {
//            bitmap.writePixels(Color.parseColor("#f6f6f6"));
            bitmap.writePixels(Color.getIntColor("#f6f6f6"));
        }

//        Canvas canvas = new Canvas(new Texture(bitmap));
//        canvas.translate(
//                -(int) (translateX * scale), -(int) (translateY * scale)
//        );
//        if (sampleFactor > 1.0f) {
//            canvas.scale(scale, scale);
//        }

        PixelMap.InitializationOptions opt1 = new PixelMap.InitializationOptions();
        if (sampleFactor > 1.0f) {
//            opt1.size.width = (int) (bitmap.getImageInfo().size.width * scale); // 宽缩放ratio
//            opt1.size.height = (int) (bitmap.getImageInfo().size.height * scale); // 搞缩放ration
            opt1.size = new Size((int) (bitmap.getImageInfo().size.width * scale), (int) (bitmap.getImageInfo().size.height * scale));
        }
        Rect srcRect = new Rect(-(int) (translateX * scale), -(int) (translateY * scale), bitmap.getImageInfo().size.width, bitmap.getImageInfo().size.height);
        opt1.editable = true;
        opt1.pixelFormat = PixelFormat.ARGB_8888;

        PixelMap bitmap1 = PixelMap.create(bitmap, srcRect, opt1);
//        view.draw(canvas);
        view.setBackground(new PixelMapElement(bitmap1));
//        view.setScale(scale,scale);
        return bitmap1;
    }

    public static PixelMap transformBitmap(PixelMap bitmap, int translateX, int translateY) {

        if (translateX == 0 && translateY == 0) {
            return bitmap;
        }
        PixelMap.InitializationOptions opt = new PixelMap.InitializationOptions();
        opt.size = bitmap.getImageInfo().size;
        opt.editable = true;
        opt.pixelFormat = PixelFormat.ARGB_8888;
        Rect srcRegion = new Rect(translateX, translateY,
                bitmap.getImageInfo().size.width - translateX, bitmap.getImageInfo().size.height - translateY);
//        return Bitmap.createBitmap(bitmap, translateX, translateY, bitmap.getWidth() - translateX, bitmap.getHeight() - translateY);
        return PixelMap.create(bitmap, srcRegion, opt);
    }


    public static synchronized native void replaceBitmap(PixelMap bitmap, int[] pixels, int x, int y, int deltaX, int deltaY);

    static {
        System.loadLibrary("hoko_blur");
    }
}
